<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    指针数组和指向指针的指针 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						指针数组和指向指针的指针
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/19 21:26 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>指针数组</h2>
<p>所有元素均为指针类型数据的数组，称为指针数组，也就是说，指针数组中的每一个元素都相当于一个指针变量</p>
<h3><a id="%E4%B8%80%E7%BB%B4%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E7%9A%84%E5%AE%9A%E4%B9%89%E5%BD%A2%E5%BC%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>一维指针数组的定义形式</h3>
<pre class="line-numbers"><code class="language-c">类型 *数组名[数组长度]
int *p[4];
</code></pre>
<ul>
<li>指针数组的元素存储的是该元素指向的地址</li>
</ul>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;

int main()
{
    int *p[3];
    int a, b, c;
    p[0] = &amp;a;
    p[1] = &amp;b;
    p[2] = &amp;c;
    a = 10, b = 20, c = 30;

    printf(&quot;p[0] = %d\n&quot;, p[0]); // p[0]输出的是该元素指向的地址
    printf(&quot;*p[0] = %d\n&quot;, *p[0]); // *p[0]输出的是该元素指向的地址内存储的内容
    return 0;
}
</code></pre>
<p><strong>输出结果</strong></p>
<pre class="line-numbers"><code class="language-c">p[0] = 1833318056
*p[0] = 10
</code></pre>
<h3><a id="%E4%BE%8B" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>例</h3>
<p>将若干字符串按字母顺序（从小到大）输出</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main()
{
    void sort(char *name[], int n);
    char *name[] = {
        &quot;follow me&quot;,
        &quot;basic&quot;,
        &quot;great wall&quot;,
        &quot;computer design&quot;,
        &quot;fortran&quot;
    };
    int n = 5;
    sort(name, n);
    for (int i = 0; i &lt; n; i++)
        printf(&quot;%s\n&quot;, name[i]);
    return 0;
}

void sort(char *name[], int n)
{
    char *temp; // 字符串指针，用于比较排序时交换数组元素的顺序
    int i, j, pos;
    for (i = 0; i &lt; n - 1; i++)
    {
        pos = i;
        for (j = i + 1; j &lt; n; j++)
        {
            if (strcmp(name[pos], name[j]) &gt; 0) //用strcmp()函数对字符串进行比较，如果name[pos]&gt;name[j]，返回一个正整数
                pos = j;
        }
        if (pos != i)
        {
            temp = name[i];
            name[i] = name[pos];
            name[pos] = temp;
        }
    }
}
</code></pre>
<h2><a id="%E6%8C%87%E5%90%91%E6%8C%87%E9%92%88%E7%9A%84%E6%8C%87%E9%92%88" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>指向指针的指针</h2>
<p>定义一个指向指针数据的指针变量：</p>
<pre class="line-numbers"><code class="language-c">char **p;
</code></pre>
<ul>
<li><code>p</code>的前面有两个<code>*</code>号。<code>*</code>运算符从右往左运算，因此<code>**p</code>等价于<code>*(*p)</code>，<code>*p</code>是指针变量的定义形式，<code>**p</code>是指向<code>*p</code>指针的地址的指针</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>